package net.bitdixit.ywu.preferences;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class AppPreferences
{	
    private static final String APP_SHARED_PREFS = "net.bitdixit.ywu_preferences"; //  Name of the file -.xml
	
	SharedPreferences preferences;
	Editor            editor;
	ArrayList<String> staredAsanasCache;
	
    public AppPreferences(Context context)
    {
    	preferences = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
        editor = preferences.edit();
		editor.commit();
		staredAsanasCache=null;
    }	

    public int delayToFirstBell()
	{
		return Integer.parseInt(preferences.getString("delayToFirstBell", "10"));
	}
	public int alertBell1()
	{
		return Integer.parseInt(preferences.getString("alertBell1", ""+60));
	}
	public int alertBell2()
	{
		return Integer.parseInt(preferences.getString("alertBell2", ""+(5*60)));
	}
	public int alertBell3()
	{
		return Integer.parseInt(preferences.getString("alertBell3", ""+(10*60)));
	}
	public boolean getSetAirplaneMode()
	{
		return preferences.getBoolean("airplaneMode", false);
	}
	public int getMultimediaLevel()
	{
		return Integer.parseInt(preferences.getString("multimediaLevel", "-1"));
	}
	public boolean isAsanaStared(String asanaId)
	{
		return getStaredAsanasList().contains(asanaId);
	}
	public void setAsanaStared(String asanaId, boolean stared)
	{
		ArrayList<String> asanas= getStaredAsanasList();
		if (asanas.contains(asanaId) && stared==false) asanas.remove(asanaId);
		else if (!asanas.contains(asanaId) && stared==true) asanas.add(asanaId);
		setStaredAsanasList(asanas);
	}
	public ArrayList<String> getStaredAsanasList()
	{
		ArrayList<String> list = new ArrayList<String>();
		String string = preferences.getString("staredAsanas", null);
		if (string==null) return list;
		for (String s :  string.split("\\|")) if(s.length()>0) list.add(s);
		return list;			
	}
	private void setStaredAsanasList(ArrayList<String> asanas)
	{
		StringBuffer s = new StringBuffer();
		for (String asana : asanas)
		{
			if (s.length()>0) s.append("|");
			s.append(asana);
		}
		editor.putString("staredAsanas",s.toString());
		editor.commit();
	}
	public void setAsanaLibraryViewPreferences(String prefs)
	{
		editor.putString("asanaLibraryViewPreferences",prefs);
		editor.commit();		
	}
	public String getAsanaLibraryViewPreferences()
	{
		return preferences.getString("asanaLibraryViewPreferences", null);		
	}
}
